﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;

namespace AMM.Business {
    public class PowerHelper {
        public int AppendPowers(params Powers[] powers) {
            int result = 0;
            powers.ToList().ForEach(x => {
                result += (int)x;
            });
            return result;
        }

        public List<Powers> FetchPowers(int power) {
            List<Powers> powerList = new List<Powers>();
            string binaryString = Convert.ToString(power, 2);
            int currentNumber, powFor2;
            for (int i = 0; i < binaryString.Length; i++) {
                currentNumber = int.Parse(binaryString.Substring(i, 1));
                if (currentNumber == 0) { continue; }
                powFor2 = (int)Math.Pow(2, binaryString.Length - i - 1);
                powerList.Add((Powers)(currentNumber * powFor2));
            }
            return powerList.Reverse<Powers>().ToList();
        }

        public string GetPowerDescription(Powers power) {
            return MethodHelper.GetEnumsDescription(typeof(Powers), (int)power);
        }

        public string GetPowerDescriptions(int power) {
            Type type;
            Powers powerValue;
            string result = string.Empty;
            List<Powers> powerList = this.FetchPowers(power);
            MethodHelper.GetEnumDescriptionToList(typeof(Powers)).ForEach(x => {
                type = x.GetType();
                powerValue = (Powers)type.GetProperty("Value").GetValue(x, null);
                if (powerList.Contains(powerValue)) {
                    result += "，" + x.GetType().GetProperty("Text").GetValue(x, null);
                }
            });
            return result.Trim('，');
        }
    }    
}
